package com.ujiuye.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

/*
* 数据库连接池工具类
* */
public class DruidUtils {

    //获取数据源
    public static DataSource getDataSource(){
        InputStream in = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");

        Properties prop = new Properties();
        DataSource dataSource = null;

        try {
            prop.load(in);

            //创建数据源
             dataSource= DruidDataSourceFactory.createDataSource(prop);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dataSource;
    }

    /*
    * 创建dbUtils 的核心操作工具QuerRunner 对象,并传递数据源
    * */
    public static QueryRunner getQueryRunner() {
        return new QueryRunner(getDataSource());
    }


    @Test
    public void test() throws SQLException {
        System.out.println(getDataSource().getConnection());
    }
}
